package cn.kogee.basic.controller;

import cn.kogee.basic.dto.BinderSmsDto;
import cn.kogee.basic.dto.SsmDto;
import cn.kogee.basic.exception.BusinessException;
import cn.kogee.basic.service.IVerifyImageService;
import cn.kogee.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyImageController {
    @Autowired
    private IVerifyImageService verifyImageService;

    /**
     * 手机注册图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public AjaxResult image(@PathVariable("key") String key){
        try {
            String base64Code=verifyImageService.Verification(key);
            return AjaxResult.me().setResultObj(base64Code);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("获取验证码失败");
        }
    }

    /**
     * 短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult phoneregister(@RequestBody SsmDto dto){
        try {
            verifyImageService.smsCode(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("网络繁忙，稍后重试");
        }
    }
    /**
     * 微信短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody BinderSmsDto dto){
        try {
            verifyImageService.binderLogin(dto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("网络繁忙，稍后重试");
        }
    }

    /**
     * 邮箱图形验证码
     * @param key
     * @return
     */
    @GetMapping("/email/{key}")
    public AjaxResult emailimage(@PathVariable("key") String key){
        try {
            String base64Code=verifyImageService.EmailVerification(key);
            return AjaxResult.me().setResultObj(base64Code);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMsg("获取验证码失败");
        }
    }
}
